package com.gameguru.mathgame;

import java.io.IOException;
import java.util.HashSet;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
	public static final String ID = "_id";
	public static final String P_NAME = "playerName";
	public static final String TOT_Q = "TotalQuestion";
	public static final String SCORE = "Score";
	public static final String TEST_TYPE ="TestType";
	public static final String PLAY_DATE = "PlayDate";
	public static final String DB_NAME="mathGame";
	public static final String SCORE_TABLE = "scoredb";
	public static final String TIME_TAKEN = "timetaken";
	public static String DB_PATH="";
	public static SQLiteDatabase myDB;
	//public static Context myContext;

	public DBHelper(Context context) {
		super(context, DB_NAME, null, 1);
	}
	public void createDatabase() throws IOException {
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table if not exists " + 
				SCORE_TABLE + "(_id integer primary key autoincrement, " +
				P_NAME + " text, " + 
				TOT_Q + " integer , " + 
				SCORE + " integer, " + 
				TEST_TYPE + " integer, " + 
				TIME_TAKEN + " integer, " +
				PLAY_DATE + " integer);"
		);
	}
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
	}

	public void save(ContentValues contentValues ){
		try {
			myDB = getWritableDatabase();
			/*String sql = "insert into "+SCORE_TABLE+ "( '"+P_NAME+"','"+ TOT_Q +"','"+
			SCORE +"','"+TIME_TAKEN +"','"+PLAY_DATE+"') values('"+results.get(0)+"'," +
			results.get(1) + "," + results.get(2) +"," + results.get(3) +",'" + results.get(4)  +"');";*/
			//contentValues.put("playerName", "playerName");
			myDB.insert(SCORE_TABLE, null, contentValues);			
			Cursor mCursor = getReport(contentValues.getAsString(P_NAME));
			if (mCursor != null) {
				while(!mCursor.isAfterLast()) {
				Log.d("OUTPUT",mCursor.getString(mCursor.getColumnIndex(TEST_TYPE)));
				mCursor.moveToNext();
				}
			} else {
				// No data to display
			}

		}catch(Exception e)  {
			Log.e("mathGame",e.toString());
		} finally {
			close();
		}
	}


	public Cursor getReport(String playerName)  {
		Cursor mCursor=null;
		try {
			this.open();
			playerName = (playerName== null) ? Constants.GUEST:playerName;
			mCursor = myDB.query(true, SCORE_TABLE, new String[] {ID,
					P_NAME, TOT_Q, SCORE,TEST_TYPE ,TIME_TAKEN,PLAY_DATE },
					P_NAME + "='" + playerName+"'", null, null, null, null, null);
			if (mCursor != null) {
				mCursor.moveToFirst();
			}	
			
		} catch (Exception e) {
			
			Log.e("mathGame",e.toString());
		} finally {
			this.close();
		}
		Log.d("Cursor","Cursor count" +mCursor.getCount());
		return mCursor;
	}
	public Cursor getReport(String playerName, int testType)  {
		Cursor mCursor=null;
		Cursor mCursor1=null;
		try {
			this.open();
			playerName = (playerName== null) ? Constants.GUEST:playerName;
			mCursor = myDB.query(true, SCORE_TABLE, new String[] {ID,
					P_NAME, TOT_Q, SCORE,TEST_TYPE ,TIME_TAKEN,PLAY_DATE },
					P_NAME + "='" + playerName+"' and "+ TEST_TYPE + "=" + testType, null, null, null, null, null);
			String sql = "select * from scoredb where playerName = '" + playerName + "' and TestType = " + testType;
			mCursor1 = myDB.rawQuery(sql, null);
			if (mCursor != null) {
				mCursor.moveToFirst();
			}	
			
		} catch (Exception e) {
			
			Log.e("mathGame",e.toString());
			Log.e("mCursor1",mCursor1.getCount()+"");
		} finally {
			this.close();
		}
		Log.d("Cursor","Cursor count" +mCursor.getCount());
		return mCursor;
	}
	public Cursor getAllReport(String playerName, int testType)  {
		Cursor mCursor=null;
		
		try {
			this.open();
			playerName = (playerName== null) ? Constants.GUEST:playerName;			
			String sql = "select * from scoredb where playerName = '" + playerName + "' and TestType = " + testType + ";";
			mCursor = myDB.rawQuery(sql, null);
			if (mCursor != null) {
				mCursor.moveToFirst();
			}	
			
		} catch (Exception e) {
			
		} finally {
			this.close();
		}
		Log.d("Cursor","Cursor count" +mCursor.getCount());
		return mCursor;
	}
	
	public SQLiteDatabase open()  {
		try {
			myDB = getWritableDatabase();
		}catch(Exception e)  {
			Log.e("mathGame",e.toString());
		} 
		return myDB;
	}
	public void close() {
		if (myDB!=null){
			myDB.close();
		}
		
	}
	public HashSet<String> getPlayerList() {
		Cursor mCursor=null;
		HashSet<String> playerList=new HashSet<String>();	
		try {
			this.open();
			mCursor=myDB.query(SCORE_TABLE, new String[]{P_NAME}, null, null, null, null, P_NAME, null);
/*			mCursor = myDB.query(true, SCORE_TABLE, new String[] {
					P_NAME},
					null, null, null, null, null, null);*/
			if (mCursor != null) {								
				mCursor.moveToFirst();
				while(!mCursor.isAfterLast()) {
					playerList.add(mCursor.getString(mCursor.getColumnIndex(DBHelper.P_NAME)));
					mCursor.moveToNext();
					}
			}
			
		} catch (Exception e) {			
			Log.e("mathGame",e.toString());
		} finally {
			this.close();
		}
		Log.d("Cursor","Cursor count" +mCursor.getCount() +"~"+ playerList.size());
		return playerList;
	}
	

}
